﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Classwork08
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string type = null;
        double x = 0;
        double y = 0;
        bool c = false;
        

        private void button1_Click(object sender, EventArgs e)
        {
            if (c == true)
            {
                c = false;
                textBox1.Text = " ";
            }
            textBox1.Text += "1";
            textBox2.Text += "1";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (c == true)
            {
                c = false;
                textBox1.Text = " ";
            }
            textBox1.Text += "2";
            textBox2.Text += "2";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (c == true)
            {
                c = false;
                textBox1.Text = " ";
            }
            textBox1.Text += "3";
            textBox2.Text += "3";
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (c == true)
            {
                c = false;
                textBox1.Text = " ";
            }
            textBox1.Text += "4";
            textBox2.Text += "4";
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (c == true)
            {
                c = false;
                textBox1.Text = " ";
            }
            textBox1.Text += "5";
            textBox2.Text += "5";
        }

        private void button7_Click(object sender, EventArgs e)
        {
            if (c == true)
            {
                c = false;
                textBox1.Text = " ";
            }
            textBox1.Text += "6";
            textBox2.Text += "6";
        }

        private void button9_Click(object sender, EventArgs e)
        {
            if (c == true)
            {
                c = false;
                textBox1.Text = " ";
            }
            textBox1.Text += "7";
            textBox2.Text += "7";
        }

        private void button10_Click(object sender, EventArgs e)
        {
            if (c == true)
            {
                c = false;
                textBox1.Text = " ";
            }
            textBox1.Text += "8";
            textBox2.Text += "8";
        }

        private void button11_Click(object sender, EventArgs e)
        {
            if (c == true)
            {
                c = false;
                textBox1.Text = " ";
            }
            textBox1.Text += "9";
            textBox2.Text += "9";
        }

        private void button13_Click(object sender, EventArgs e)
        {
            /*string str1 = textBox1.Text.Substring(textBox1.Text.Length - 1);*/
            if (c == true)
            {
                c = false;
                textBox1.Text = " ";
            }
            if(textBox1.Text!=" 0")
            {
                textBox1.Text += "0";
                textBox2.Text += "0";
            }
        }

        private void button14_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.IndexOf(".") == -1)
            {
                textBox1.Text += ".";
                textBox2.Text += ".";
            }
        }

        private void button15_Click(object sender, EventArgs e)
        {
            textBox1.Text = " ";
            textBox2.Text = " ";
            type = null;
            x = 0;
            y = 0;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if(type == null && textBox2.Text != " ")
            {
                c = true;
                type = "+";
                textBox2.Text += "+";
                x = double.Parse(textBox1.Text);
            }
        }

        private void button8_Click(object sender, EventArgs e)
        {
            if (type == null && textBox2.Text != " ")
            {
                c = true;
                type = "-";
                textBox2.Text += "-";
                x = double.Parse(textBox1.Text);
            }
        }

        private void button12_Click(object sender, EventArgs e)
        {
            if (type == null && textBox2.Text != " ")
            {
                c = true;
                type = "*";
                textBox2.Text += "*";
                x = double.Parse(textBox1.Text);
            }
        }

        private void button16_Click(object sender, EventArgs e)
        {
            if (type == null && textBox2.Text != " ")
            {
                c = true;
                type = "/";
                textBox2.Text += "/";
                x = double.Parse(textBox1.Text);
            }
        }

        private void button17_Click(object sender, EventArgs e)
        {
            y = double.Parse(textBox1.Text);
            c = true;

            if (type == "+")
            {
                textBox1.Text = (x + y).ToString();
                textBox2.Text += "=" + textBox1.Text;
            }
            if (type == "-")
            {
                textBox1.Text = (x - y).ToString();
                textBox2.Text += "=" + textBox1.Text;
            }
            if (type == "*")
            {
                textBox1.Text = (x * y).ToString();
                textBox2.Text += "=" + textBox1.Text;
            }
            if (type == "/")
            {
                if (y != 0)
                {
                    textBox1.Text = (x / y).ToString();
                    textBox2.Text += "=" + textBox1.Text;
                }
                else
                {
                    MessageBox.Show("请重新输入", "错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    textBox1.Text = " ";
                    textBox2.Text = " ";
                }
            }
            type = null;
            x = 0;
            y = 0;
        }
    }
}
